package cn.fourhundredandeighteen.controller;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.dao.StudentDao;
import cn.fourhundredandeighteen.entity.*;
import cn.fourhundredandeighteen.service.*;
import cn.fourhundredandeighteen.service.impl.*;
import cn.fourhundredandeighteen.utill.AdminCheck;
import cn.fourhundredandeighteen.utill.MysqlConnect;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.utill.ResultGenerator;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:学生信息处理中心
 * @author: liuyongqi
 * @time: 2020/12/16
 */
public class StudentController {
    public Result<List<Student>> getAllStudent(){
        StudentService studentService=new StudentServiceImpl();
        try {
            List<Student> students = studentService.selectAllStudent();
            if (students!=null){
                return ResultGenerator.successResult(students);
            }
            return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 学生个人信息修改
     * @param student 学生对象
     * @return Result
     * 功能已添加
     */
    public Result upDataStudentInfo(Student student){
        StudentService service=new StudentServiceImpl();
        int i = service.upDataStudentInfo(student);
        if (i==1){
            return ResultGenerator.successResult();
        }else {
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 学生信息录入，老师操作类--创建学生
     * @param student 学生信息对象
     * @return
     */
    public Result saveStudentInfo(Student student){
      StudentService service=new StudentServiceImpl();
      try {
          int save = service.save(student);
          if (save>0)
              return ResultGenerator.successResult();
          else
              return ResultGenerator.errorResult();
      }catch (Exception e){
          e.printStackTrace();
          return ResultGenerator.errorResult();
      }
    }

    /**
     * 学生详细信息查询 --个人信息
     * @param studentId 学生学号
     * @return
     * 功能已添加
     */
    public Result<Student> studentInfo(Long studentId){
        StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
        try {
            Student student = studentDao.selectByPrimaryKey(studentId);
            if (student!=null){
                return ResultGenerator.successResult(student);
            }else
                return ResultGenerator.errorResult();

        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /** 学生个人信息删除-管理员
     * @param studentId
     * @return
     */
    public Result delStudent(Long studentId){
        StudentService service=new StudentServiceImpl();
        try {
            int delete = service.delete(studentId);
            if (delete>0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    //其他操作
    /**
     * 学生选择课程--选课
     * @param courseId
     * @param studentId
     * @return 是否选中
     * 功能已添加
     */
    public Result studentSelectCourse(Long courseId,Long studentId){
        StudentService service=new StudentServiceImpl();
        CourseController controller=new CourseController();
        try {
            //课程是否存在
            Result<Course> courseId1 = controller.getCourseId(courseId);
            if (courseId1.getCode()==500){
                return ResultGenerator.errorResult();
            }
            Boolean aBoolean = service.studentSelectCourse(courseId, studentId);
            if (aBoolean){
                System.out.println(1);
                return ResultGenerator.successResult();
            }else
                System.out.println(2);
                return ResultGenerator.errorResult();
        }catch (Exception e){
            System.out.println(3);
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 学生未选课程信息查询--未选信息
     * @param studentId
     * @return 未选信息
     * 功能已添加
     */
    public Result<List<Course>> getCourseInfo(Long studentId){
        CourseService courseService=new CourseServiceImpl();
        try {
            List<Course> courses = courseService.selectCourse(studentId);
            return ResultGenerator.successResult(courses);
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 学生已经选修课程信息--课程已选信息
     * @param studentId 学生学号
     * @return 学生选修课
     * 功能已添加
     */
    public Result<List<Course>> studentCourse(Long studentId){
        StudentCourseService studentCourseService=new StudentCourseServiceImpl();
        List<StudentCourse> studentCourses = studentCourseService.selectStudentToCourse(studentId);
        CourseService service=new CourseServiceImpl();
        List<Long> courseIds=studentCourses.stream().map(StudentCourse::getCourseId).collect(Collectors.toList());
        try {
            List<Course> courseList = service.sleectCourse(courseIds);
            return ResultGenerator.successResult(courseList);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    /**学生课程成绩查询
     * @param studentId 学生id
     * @param courseId 课程id
     * @return 学生成绩
     * 功能已添加
     */
    public Result<Score> getStudentScore(Long studentId,Long courseId){
        CourseService courseService=new CourseServiceImpl();
        try {
            Course course = courseService.getCourse(courseId);
            if (!AdminCheck.adminCheck()) {
                if (!course.getTeacherId().equals(UserInfo.Session))
                    return ResultGenerator.errorResult();
            }
            Score select = courseService.select(studentId, courseId);
            if (select!=null)
                return ResultGenerator.successResult(select);
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    /**学生课程成绩录入更新
     * @param score
     * @return
     * 功能已添加
     */
    public Result setCourseScore(Score score){
        CourseService courseService=new CourseServiceImpl();
        try {
            Course course = courseService.getCourse(score.getCourseId());
            if (!AdminCheck.adminCheck()) {
                if (!course.getTeacherId().equals(UserInfo.Session))
                return ResultGenerator.errorResult();
            }
            //成绩录入检查
            Score i1 = courseService.checkScore(score.getStudentId(), score.getCourseId());
            if (i1 != null) {
                System.out.println("成绩存在");
                i1.setScore(score.getScore());
                int i = courseService.upScore(i1);
                if (i > 0)
                    return ResultGenerator.successResult();
                else
                    return ResultGenerator.errorResult();
            } else {
                int i = courseService.setScore(score);
                if (i > 0)
                    return ResultGenerator.successResult();
                else
                    return ResultGenerator.errorResult();
            }
            }catch(Exception e){
                e.printStackTrace();
                return ResultGenerator.errorResult();
            }
    }
    //学生课程成绩修改


    //学生奖项操作
    /**学生个人获奖信息查询--比赛信息
     * @return
     * 功能已添加
     */
    public Result<List<Subject>> studentSubjectInfo(){
        //学生id
        Long studentId = UserInfo.Session;
        SubjectStudentService subjectStudentService=new SubjectStudentServiceImpl();
        SubjectService subjectService=new SubjectServiceImpl();
        try {
            //获取获奖信息
            List<Long> studentToSubjectList = subjectStudentService.getStudentToSubjectList(studentId);
            //无获奖信息直接返回错误
            if (studentToSubjectList==null)
                return ResultGenerator.errorResult();
            //获取奖项信息
            List<Subject> subjectInfoList = subjectService.getSubjectInfoList(studentToSubjectList);
            if (subjectInfoList==null)
                return ResultGenerator.errorResult();
            return ResultGenerator.successResult(subjectInfoList);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 取消课程选择
     * @param studentId
     * @param courseId
     * @return
     * 功能已添加
     */
    public Result cancelCourse(Long courseId,Long studentId){
        CourseService courseService=new CourseServiceImpl();
        StudentService service=new StudentServiceImpl();
        try {
            //验证
            Student student = service.selectStudent(studentId);
            if (!AdminCheck.adminCheck()){
                if (!student.getStudentId().equals(UserInfo.Session))
                    return ResultGenerator.errorResult();
            }
            //取消关联
            int i = courseService.cancelCourse(studentId, courseId);
            if (i>0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }
}
